package com.cloudea.learn.layer;

import com.cloudea.learn.Tensor;

/**
 * leakyrelu激活函数
 * @author cloudea
 * @date 2022/01/03
 */
public class LeakyRelu extends Layer {

    @Override
    protected Tensor forward(Context context, Tensor... inputs) {
        return Operator.leakyRelu(inputs[0]);
    }

    @Override
    public Tensor[] backward(Context context, Tensor grid, Tensor... inputs) {
        Tensor x = inputs[0];
        Tensor gridX = new Tensor(x.getShape());
        for(int i = 0; i < x.getSize(); i++){
            gridX.setByLocation(i, grid.getByLocation(i) *  (x.getByLocation(i) > 0.0 ? 1.0 : 0.01));
        }
        return new Tensor[]{gridX};
    }
}
